package com.elastic.mapping;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

public class ClientFactory {

	private static Client client;


	public static Client getInstance() {
		// 9300 是我们java 连接的esserver 的端口。9200 是restapi 的接口
		Settings settings = ImmutableSettings.settingsBuilder()
				.put("client.transport.sniff", true)
				.put("client", true)
				.put("data", false)
				// .put("path.data",indexPath)
				.put("cluster.name", "elasticsearch")
				.put("index.unassigned.node_left.delayed_timeout", "5m")
				.put("client.transport.ping_timeout", "1000s").build();

		if(client == null){
			client = new TransportClient()
			.addTransportAddress(new InetSocketTransportAddress(
					"127.0.0.1", 9300));
		}
		return client;
	}
	
	public static Client getInstanceRemote() {
		// 9300 是我们java 连接的esserver 的端口。9200 是restapi 的接口
		Settings settings = ImmutableSettings.settingsBuilder()
				.put("client.transport.sniff", true).put("client", true)
				.put("data", false)
				// .put("path.data",indexPath)
				.put("cluster.name", "fxtweb_search")
				.put("client.transport.ping_timeout", "10000s").build();

		if(client == null){
			//默认不加入设置的话，集群的名称就是elasticsearch
			client = new TransportClient(settings)
			.addTransportAddress(new InetSocketTransportAddress(
					"192.168.1.112", 9300));
		}
		return client;
	}
	public static void close(){
		if(client!=null){
			client.close();
		}
	}
}
